package com.example.demo;


import net.sf.json.JSONObject;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;

@RestController
public class GetStockData {
    @RequestMapping("/stock")
    public String hello() throws UnsupportedEncodingException {
        System.out.println("hello,world!!!");

        String cmdStr = "  ";
        // String canshu = "{\"api_name\": \"stock_basic\", \"token\": \"531f7eda35d265a43c7091a5971802aab320a80c16afb6e7164c595a\", \"params\": {\"list_stauts\":\"L\"}, \"fields\": \"ts_code,name,area,industry,list_date\"}";
        /// String canshu = "'{\"api_name\": \"index_basic\", \"token\": \"531f7eda35d265a43c7091a5971802aab320a80c16afb6e7164c595a\", \"params\": {\"market\":\"SW\"}, \"fields\": \"ts_code,name,market\"}'";
        // String url = "http://api.waditu.com";
       //curl -X POST -d " -H "Content-Type:application/json; charset=utf-8"
        String url="http://api.waditu.com";
        String canshu=setStockParameter("index_basic");
        String[] cmds = {"curl","-X","POST","-d",canshu,"-H \"Content-Type:application/json; charset=GBK\"","http://api.waditu.com"};


      //  String sb = execCurl(cmds);
       /// String s = new String(sb.toString().getBytes("gbk"),"gbk");
        System.out.println(execCurl(cmds).toString());
        return execCurl(cmds).toString();

    }
    public String setStockParameter(String name){
        String key="531f7eda35d265a43c7091a5971802aab320a80c16afb6e7164c595a";
        //String canshu="{\\\"api_name\\\": \\\""+name+"\\\", \\\"token\\\": \\\""+key+"\\\", \\\"params\\\": {\\\"market\\\":\\\"SW\\\"}, \\\"fields\\\": \\\"ts_code,name,market\\\"}";

        //String canshu="\"{\"api_name\": \"stock_basic\", \"token\": \"531f7eda35d265a43c7091a5971802aab320a80c16afb6e7164c595a\", \"params\": {\"list_stauts\":\"L\"}, \"fields\": \"ts_code,name,area,industry,list_date\"}\" http://api.waditu.com";

        String canshu="{\"api_name\": \"stock_basic\", \"token\": \"531f7eda35d265a43c7091a5971802aab320a80c16afb6e7164c595a\", \"params\": {\"list_stauts\":\"L\"}, \"fields\": \"ts_code,name,area,industry,list_date\"}";
        /*        try{
            StringBuilder sb=new StringBuilder(s);
          //  sb.append(s);
            System.out.println("sb.toString()........."+sb.toString());
            JSONObject jsonObject = JSONObject.fromObject(sb.toString());
            String canshu2 = jsonObject.toString();
            System.out.println("==============canshu2======================="+canshu2);
            return canshu2;
        }catch (Exception e){
            System.out.println(e);
        }*/
        return canshu;

    }
    public JSONObject execCurl(String[] cmds) {
        System.out.println(Arrays.toString(cmds));

        ProcessBuilder process = new ProcessBuilder(cmds);
        Process p;
        try {
            p = process.start();
           // BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));

            BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream(),"GBK"));
            StringBuilder builder = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
                // System.out.println(line);
                builder.append(System.getProperty("line.separator"));
            }

            JSONObject jsonObject = JSONObject.fromObject(builder.toString());
            return jsonObject;

        } catch (IOException e) {
            System.out.print("error");
            e.printStackTrace();
        }
        return null;

    }

}
